<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="包厢名称:">
              <el-input
                clearable
                v-model="where.name"
                placeholder="包厢名称"/>
            </el-form-item>
          </el-col>


          <div class="ele-form-actions">
            <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="reload">查询
            </el-button>
            <el-button @click="reset">重置</el-button>
          </div>

        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 115px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            @click="openEdit(null)"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            size="small"
            v-if="permission.includes('sys:index:add')">添加
          </el-button>

          <el-button
            @click="removeBatch"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            size="small"
            v-if="permission.includes('sys:index:dall')">删除
          </el-button>
        </template>
        <template slot="status" slot-scope="{row}">
          <el-switch
            v-model="row.status"
            @change="status(row)"
            :active-value="1"
            :inactive-value="2"/>
        </template>
        <template slot="status" slot-scope="{row}">
          <el-tag v-if="row.type === 1" type="success" size="small">启用</el-tag>
          <el-tag v-if="row.type === 0" type="danger" size="small">禁用</el-tag>

        </template>
        <template slot="subscribe" slot-scope="{row}">
          <el-tag v-if="row.type === 1" type="success" size="small">启用</el-tag>
          <el-tag v-if="row.type === 0" type="danger" size="small">禁用</el-tag>

        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch
            v-model="row.status"
            @change="editStatus(row)"
            :active-value="1"
            :inactive-value="0"/>
        </template>
        <template slot="subscribe" slot-scope="{row}">
          <el-switch
            v-model="row.subscribe"
            @change="editSubscribe(row)"
            :active-value="1"
            :inactive-value="0"/>
        </template>
        <template slot="is_status" slot-scope="{row}">
          <el-switch
            v-model="row.is_status"
            @change="setpush(row)"
            :active-value="1"
            :inactive-value="2"/>
        </template>

        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:index:edit')">修改
          </el-link>

          <el-popconfirm
            class="ele-action"
            title="确定要删除此数据吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:index:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>



      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <member-edit
      :data="current"
      :cate-list="cateList"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import MemberEdit from './member-edit';

export default {
  name: 'Member',
  components: {MemberEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/course/index',
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',

        // },
        // {
        //   prop: 'id',
        //   label: 'ID',
        //   width: 60,
        //   align: 'center',
        //   showOverflowTooltip: true,

        // },

        {
          prop: 'name',
          label: '包厢名称',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,

        },
        {
          prop: 'door_name',
          label: '门禁名称',
          width: 100,
          align: 'center',
          showOverflowTooltip: true,

        },
        {
          prop: 'package_name',
          label: '已选套餐',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,

        },
        {
          prop: 'status',
          label: '状态',
          align: 'center',
          width: 80,
          resizable: false,
          slot: 'status',
        },
        {
          prop: 'subscribe',
          label: '显示已预约',
          align: 'center',
          width: 80,
          resizable: false,
          slot: 'subscribe',
        },



        {
          prop: 'create_time',
          label: '添加时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 160,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',

        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 栏目列表
      cateList: []
    };
  },
  mounted() {


  },
  methods: {
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },


    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/course/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据')
        return;
      }
      this.$confirm('确定要删除选中的数据吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/course/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({type: 'success', message: res.data.msg});
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    openCrad: function (row) {
      this.$router.push({path: '/setcoach/setcoach?id=' + row.id})
    },
    openchang: function (row) {
      this.$router.push({path: '/storechu/storechu?id=' + row.id})
    },
    openTime: function (row) {
      this.$router.push({path: '/tool/example/time?id=' + row.id})
    },
    openTimecahng: function (row) {
      this.$router.push({path: '/tool/example/timechang?id=' + row.id})
    },
    openCyclecahng: function (row) {
      this.$router.push({path: '/tool/example/cyclechang?id=' + row.id})
    },
    openCycle: function (row) {
      this.$router.push({path: '/tool/example/cycle?id=' + row.id})
    },


    opendate: function (row) {
      this.$router.push({path: '/tool/example/date?id=' + row.id})
    },
    opendtimechang: function (row) {
      this.$router.push({path: '/tool/example/dtimechang?id=' + row.id})
    },
    opendtime: function (row) {
      this.$router.push({path: '/tool/example/dtime?id=' + row.id})
    },
    downloadImage(src, name) { //下载图片地址和图片名
      let canvas = document.createElement('canvas')
      let img = document.createElement('img')
      img.onload = () => {
        canvas.width = img.width
        canvas.height = img.height
        let context = canvas.getContext('2d')
        context.drawImage(img, 0, 0, img.width, img.height)
        canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height)
        canvas.toBlob(blob => {
          let link = document.createElement('a')
          link.href = window.URL.createObjectURL(blob)
          link.download = 'download' // resource name
          link.click()
        }, "/*可指定图片格式*/", "0.95")
      }
      img.setAttribute("crossOrigin", 'Anonymous')
      img.src = src

    },


    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('/course/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    editSubscribe(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "subscribe": row.subscribe
      })
      this.$http.post('/course/subscribe', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          row.subscribe = !row.subscribe ? 0 : 1;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 更改状态 */
    setpush(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.is_status
      })
      this.$http.post('/course/push', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({type: 'success', message: res.data.msg});
        } else {
          row.is_status = !row.is_status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },


  }
}
</script>

<style scoped>
</style>
